package com.example.liunanxin.lnx226ordering.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.adapter.FoodAdapter;
import com.example.liunanxin.lnx226ordering.beans.FoodBean;
import com.example.liunanxin.lnx226ordering.beans.UserBean;
import com.example.liunanxin.lnx226ordering.iface.DoCollectshopListener;
import com.example.liunanxin.lnx226ordering.iface.FoodListener;
import com.example.liunanxin.lnx226ordering.iface.IsCollectListener;
import com.example.liunanxin.lnx226ordering.model.DoCollectshopModel;
import com.example.liunanxin.lnx226ordering.model.FoodModel;
import com.example.liunanxin.lnx226ordering.model.IsCollectModel;

import java.util.List;
import java.util.Locale;

public class FoodActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List<FoodBean> foodBeans;
    private FoodAdapter foodAdapter;
    private int shop_id,user_id;
    private String shopname;
    TextView tv_shop;
    private ImageView food_return;
    ImageView iv_collect;
    private SharedPreferences sharedPreferences;
    private boolean isCollect;
    private Intent intent;

    private FoodListener foodListener=new FoodListener() {
        @Override
        public void onResponse(List<FoodBean> foodBean) {
            foodBeans=foodBean;
            if (foodBeans==null)
                Toast.makeText(FoodActivity.this,"null",Toast.LENGTH_LONG).show();
            else{
                foodAdapter.setData(foodBeans);
            }
        }
        @Override
        public void onFail(String mag) {
            Toast.makeText(FoodActivity.this,mag,Toast.LENGTH_LONG).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);
        tv_shop=findViewById(R.id.textView14);
        food_return=findViewById(R.id.imageView5);
        iv_collect=findViewById(R.id.imageView6);
        shop_id=getIntent().getIntExtra("shop_id",0);
        shopname=getIntent().getStringExtra("shopname");
        tv_shop.setText(shopname);
        sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);

        user_id=Integer.parseInt(sharedPreferences.getString("userid","获取userid失败"));
        FoodModel foodModel=new FoodModel();
        foodModel.getFoodResult(shop_id,foodListener);
        initRecyclerview();
        click();
        isCollect();
        iv_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doCollect();
            }
        });
    }
    public void initRecyclerview(){
        recyclerView=findViewById(R.id.recyclerview_food);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);//增加recycleview的稳定性
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        foodAdapter=new FoodAdapter();
        recyclerView.setAdapter(foodAdapter);
    }
    private void click(){
        foodAdapter.setOnItemClickListener(new FoodAdapter.OnItemClickListener() {
            @Override
            public void setItem(View view, int position) {
                FoodBean foodBean=foodBeans.get(position);
                Intent intent=new Intent(FoodActivity.this,FoodDetailActivity.class);
                intent.putExtra("food_id",foodBean.getFood_id());
                startActivity(intent);
            }
        });
        food_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    private void isCollect() {
        IsCollectModel isCollectModel=new IsCollectModel();
        isCollectModel.isCollect(user_id,shop_id,0,isCollectListener);
    }
    private void doCollect(){
        DoCollectshopModel doCollectshopModel =new DoCollectshopModel();
        doCollectshopModel.doCollect(user_id,shop_id,doCollectshopListener);
    }
    private DoCollectshopListener doCollectshopListener=new DoCollectshopListener() {
        @Override
        public void onResponse(UserBean userBean) {
            if(userBean.getSuccess().equals("0")){
                Toast.makeText(FoodActivity.this,"收藏失败",Toast.LENGTH_SHORT).show();
            }
            else{
                if(isCollect){
                    isCollect=false;
                    iv_collect.setImageResource(R.drawable.s4);
                    Toast.makeText(FoodActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                }
                else {
                    isCollect=true;
                    iv_collect.setImageResource(R.drawable.s5);
                    Toast.makeText(FoodActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                }
            }
        }
        @Override
        public void onFail(String mag) {
            Toast.makeText(FoodActivity.this,mag,Toast.LENGTH_SHORT).show();
        }
    };
    private IsCollectListener isCollectListener=new IsCollectListener() {
        @Override
        public void onRespose(UserBean userBean) {
            if(userBean.getCollected().equals("0")){
                isCollect=false;
                iv_collect.setImageResource(R.drawable.s4);
            }
            else {
                isCollect=true;
                iv_collect.setImageResource(R.drawable.s5);
            }
        }
        @Override
        public void onFail(String mag) {
            Toast.makeText(FoodActivity.this,mag,Toast.LENGTH_SHORT).show();

        }
    };
}
